﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Net;
using Chaine.Models;
using System.Configuration;
using System.ComponentModel;
using System.Web.Security;

namespace Chaine.DAO
{
    public static class EmailManager
    {
        private static MailAddress _From;
        private static MailAddress From
        {
            get
            {
                if (_From != null)
                    return _From;
                _From = new MailAddress(ConfigurationManager.AppSettings["SMTP-FromEmail"], ConfigurationManager.AppSettings["SMTP-FromName"]);
                return _From;
            }
        }

        private static SmtpClient CreateSmtpClient()
        {
            var uri = ConfigurationManager.AppSettings["SMTP-URI"];
            var port = int.Parse(ConfigurationManager.AppSettings["SMTP-PORT"]);
            var username = ConfigurationManager.AppSettings["SMTP-UserName"];
            var password = ConfigurationManager.AppSettings["SMTP-Password"];
            var smtpClient = new SmtpClient(uri, port);
            smtpClient.EnableSsl = false;
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = new NetworkCredential(username, password);

            return smtpClient;
        }

        /// <summary>
        /// 发送注册链接的Email
        /// </summary>
        /// <param name="model"></param>
        public static void SendRegisterConfirmEmail(RegisterModel model)
        {
            var email = EmailTemplateManager.GetEmailTemplate(EmailTemplateType.VerifyEmail);
            if (email == null)
                return;
            if (model != null && !string.IsNullOrEmpty(model.Email))
            {
                var smtpClient = EmailManager.CreateSmtpClient();
                var validateURL = ConfigurationManager.AppSettings["Home"] + "/system/account/emailvalidate/" + model.Id.ToString();
                var mail = new MailMessage(EmailManager.From, new MailAddress(model.Email, model.RealName))
                {
                    Subject = email.Title,
                    Body = email.Content.Replace("{ValidateURL}", validateURL),
                    IsBodyHtml = true,
                    DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
                };
                smtpClient.Send(mail);
                mail.Dispose();
            }
        }

        public static void SendAlertNewMemberEmail()
        {
            var email = EmailTemplateManager.GetEmailTemplate(EmailTemplateType.AlertNewMember);
            if (email == null)
                return;
            var admins = AccountManager.GetAdmins();
            foreach (var admin in admins)
            {
                if (admin != null && !string.IsNullOrEmpty(admin.Email))
                {
                    var smtpClient = EmailManager.CreateSmtpClient();
                    var adminMemberURL = ConfigurationManager.AppSettings["Home"] + "/system/admin/member";
                    var mail = new MailMessage(EmailManager.From, new MailAddress(admin.Email))
                    {
                        Subject = email.Title,
                        Body = email.Content.Replace("{AdminMemberURL}", adminMemberURL),
                        IsBodyHtml = true,
                        DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
                    };
                    smtpClient.Send(mail);
                    mail.Dispose();
                }
            }

        }

        /// <summary>
        /// 一般形式发送邮件
        /// </summary>
        /// <param name="toEmail">收件地址</param>
        /// <param name="title">标题</param>
        /// <param name="content">内容</param>
        public static void SendCommonEmail(MailAddress toEmail, string title, string content)
        {

            var smtpClient = EmailManager.CreateSmtpClient();
            var mail = new MailMessage(EmailManager.From, toEmail)
            {
                Subject = title,
                Body = content,
                IsBodyHtml = true,
                DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
            };
            smtpClient.Send(mail);
            mail.Dispose();//发送完成
        }
    }
}